package music_store.ms.controller.portal;

import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.common.ServerResponse;
import music_store.ms.pojo.User;
import music_store.ms.service.IUserService;
import music_store.ms.common.ServerResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @PostMapping("login.do")
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = iUserService.login(username, password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    @PostMapping("logout.do")
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess("登出成功。");
    }

    @PostMapping("register.do")
    public ServerResponse<String> register(User user){
        return iUserService.register(user);
    }

    @PostMapping("forget_get_question.do")
    public ServerResponse<String> forgetGetQuestion(String username){
        return iUserService.selectQuestionByUsername(username);
    }

    //调用此方法，会返回一个token！用于忘记密码的重置密码。
    @PostMapping("forget_check_answer.do")
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        return iUserService.forgetCheckAnswer(username, question, answer);
    }

    //此处的token由上一个接口的返回值确定。
    @PostMapping("forget_reset_password.do")
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
        return iUserService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    //已经登录状态下，改密码。
    @PostMapping("reset_password.do")
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("未登录用户。请先登录。");
        }
        return iUserService.resetPassword(user, passwordOld,passwordNew);
    }

    @PostMapping("update_info.do")
    public ServerResponse<User> update_info(HttpSession session,User user){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ServerResponse.createByErrorMessage("未登录用户。请先登录。");
        }
        user.setId(currentUser.getId());

        return iUserService.update_info(user);
    }

    @PostMapping("get_info.do")
    public ServerResponse<User> get_info(HttpSession session){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ServerResponse.createByErrorMessage("未登录用户。请先登录。");
        }
        return iUserService.get_info(currentUser.getId());
    }



}
